package com.rzico.sbl.ui.order

import android.graphics.Bitmap
import androidx.lifecycle.lifecycleScope
import com.jeremyliao.liveeventbus.LiveEventBus
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityDeliverSignatureBinding
import com.rzico.sbl.viewmodel.DeliverViewModel
import com.xinnuo.common.extend.condition
import com.xinnuo.common.extend.get
import com.xinnuo.common.extend.notEmpty
import com.xinnuo.common.extend.oneClick
import com.xinnuo.common.helper.BitmapHelper.saveBitmapToLocal
import com.xinnuo.common.helper.getExternalCacheDirPath
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.oss.OSSHelper
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import me.jessyan.autosize.internal.CustomAdapt

class DeliverSignatureActivity : BaseActivity(R.layout.activity_deliver_signature), CustomAdapt {

    private val mBinding by lazy {
        ActivityDeliverSignatureBinding.bind(getRootView())
    }

    override fun getViewModel() = get<DeliverViewModel>()

    override fun initData() {
        setToolbarVisibility(false)
    }

    override fun initListener() {
        mBinding.apply {
            signReset.oneClick { signCanvas.clearCanvas() }

            signCancel.oneClick { finishView() }

            signSave.oneClick {
                signCanvas
                    .isBitmapEmpty
                    .condition {
                        showToast("请绘制签名信息")
                        return@oneClick
                    }

                signCanvas
                    .signatureBitmap
                    ?.let { saveBitmap(it) }
                    ?: showToast("获取签名失败")
            }
        }
    }

    /**
     * 保存图片
     */
    private fun saveBitmap(bitmap: Bitmap) {
        lifecycleScope.launch {
            showLoading()

            var filePath: String

            withContext(Dispatchers.IO) {
                filePath = bitmap.saveBitmapToLocal(
                    getExternalCacheDirPath("Sign"), "Sign_"
                )
            }

            dismissLoading()

            filePath.notEmpty {
                uploadFile(this)
            }
        }
    }

    /**
     * 上传图片
     */
    private fun uploadFile(path: String) {
        OSSHelper
            .manager
            .uploadSingleImage(
                prefix = "sign",
                path = path,
                onBefore = { showLoading("正在上传中...") },
                onAfter = { dismissLoading() },
                onSuccess = {
                    val fileUrl = it.responseHeader["url"].orEmpty()

                    // 更新运单列表
                    LiveEventBus
                        .get("signature")
                        .post(fileUrl)

                    finishView()
                },
                onError = {
                    showToast("网络异常，请稍候再试！")
                }
            )
    }

    override fun isBaseOnWidth() = true

    override fun getSizeInDp() = 640f

}